﻿using LitJson;
using UnityEngine;
public enum LoginType
{
    //与服务器连接失败
    Close,
    //成功
    Success,
    //登录失败
    Failed
}
public class LoginGlobalLogic : MonoBehaviour
{
    public int ReconnectTime = 10;
    public string channel = "appstore";
    public string version = "0.0.1";
    private bool isGetEntry = false;
    private bool closeBySelf = false;
    private string account;
    private string nickname;
    private string header;
    private int sex = 0;
    private float currentConnectTime = -1;
    private JsonData next;
    public delegate void CallBack(bool success, LoginType loginType);
    private CallBack callBack;
    void Start()
    {
        CMDConfig.Init();
    }
    public void ConnectAndLogin(string account, string nickname, CallBack callBack, string header = "", int sex = 0)
    {
        closeBySelf = false;
        isGetEntry = false;
        this.account = account;
        this.nickname = nickname;
        this.header = header;
        this.callBack = callBack;
        PomeloManager.GetInstance().Connect();
    }
    public void OnSocketClose()
    {
        if (next != null)
        {
            var next = this.next;
            this.next = null;
            isGetEntry = true;
            closeBySelf = false;
            PomeloManager.GetInstance().Connect((string)next["host"], (int)next["port"]);
        }
        else
        {
            CancelInvoke("Reconnect");
            if (!closeBySelf)
            {
                if (currentConnectTime == -1 || Time.time - currentConnectTime <= ReconnectTime)
                {
                    Invoke("Reconnect", 1);
                }
                else
                {
                    CancelInvoke("Reconnect");
                    currentConnectTime = -1;
                    callBack(false, LoginType.Close);
                    closeBySelf = true;
                    callBack = null;
                }
            }
        }
    }
    public void OnSocketOpen()
    {
        CancelInvoke("Reconnect");
        currentConnectTime = -1;
        if (!isGetEntry)
        {
            PomeloManager.GetInstance().Request(CMDConfig.REQUEST_ENTRY, OnPomeloRequestEntry);
        }
        else
        {
            JsonData jsonData = new JsonData();
            jsonData["account"] = account;
            jsonData["nickname"] = nickname;
            jsonData["device"] = "Windows PC";
            jsonData["channel"] = channel;
            jsonData["header"] = header;
            jsonData["sex"] = sex;
            jsonData["version"] = version;
            PomeloManager.GetInstance().Request(CMDConfig.REQUEST_LOGIN, jsonData, OnPomeloRequestLogin);
        }
    }
    private void Reconnect()
    {
        ConnectAndLogin(account, nickname, callBack, header, sex);
        if (currentConnectTime == -1)
        {
            currentConnectTime = Time.time;
        }
    }
    private void OnPomeloRequestEntry(JsonData response)
    {
        closeBySelf = true;
        if ((int)response["code"] != 200)
        {
            OnLoginFailed(response["data"]);
        }
        else
        {
            next = response;
        }
        PomeloManager.GetInstance().Close();
    }
    private void OnPomeloRequestLogin(JsonData response)
    {
        callBack(true, LoginType.Success);
        callBack = null;
    }
    private void OnLoginFailed(JsonData data)
    {
        callBack(false, LoginType.Failed);
        callBack = null;
    }
    void OnDestroy()
    {
        CancelInvoke();
        PomeloManager.GetInstance().Close();
    }
}
